package com.shbaiche.bifenba.adapter.data;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.data.PlayerGroupVo;
import com.shbaiche.bifenba.model.data.PlayerVo;
import com.shbaiche.bifenba.view.CircularImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.kit.KnifeKit;
import cn.droidlover.xdroidbase.imageloader.ILFactory;

/**
 * 球队成员列表
 */

public class DataTeamDetailMemberListAdapter extends SimpleRecAdapter<PlayerGroupVo, DataTeamDetailMemberListAdapter.ViewHolder> {
    private Context mContext;

    public DataTeamDetailMemberListAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public ViewHolder newViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_data_team_detail_member_list;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PlayerGroupVo item = data.get(position);

        holder.tvType.setText(item.getTitle());
        if(item.getList().size()>0){
            addPlayers(item.getList(),holder.layoutContent);
        }else{
            holder.layoutContent.removeAllViewsInLayout();
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getRecItemClick() != null) {

                }
            }
        });
    }


    private void addPlayers(List<PlayerVo> list, LinearLayout layout){
        if(list!=null && list.size()>0){
            layout.removeAllViewsInLayout();

            int line = list.size()/2;
            PlayerVo itemA,itemB;
            for (int i = 0 ;i <line;i++){
                itemA = list.get(i*2);
                itemB = list.get(i*2+1);

                View view = View.inflate(mContext,R.layout.layout_team_member_item,null);
                CircularImage ivLogoA = (CircularImage) view.findViewById(R.id.iv_logo_a);
                CircularImage ivLogoB = (CircularImage) view.findViewById(R.id.iv_logo_b);

                TextView tvNameA= (TextView)view.findViewById(R.id.tv_name_a);
                TextView tvNameB= (TextView)view.findViewById(R.id.tv_name_b);

                TextView tvNoA= (TextView)view.findViewById(R.id.tv_no_a);
                TextView tvNoB= (TextView)view.findViewById(R.id.tv_no_b);

                if(!TextUtils.isEmpty(itemA.getPhoto())){
                    ILFactory.getLoader().loadNet(ivLogoA, itemA.getPhoto(), null);
                }else{
                    ivLogoA.setImageResource(R.drawable.ic_my_head);
                }

                if(!TextUtils.isEmpty(itemB.getPhoto())){
                    ILFactory.getLoader().loadNet(ivLogoB, itemB.getPhoto(), null);
                }else{
                    ivLogoB.setImageResource(R.drawable.ic_my_head);
                }

                tvNameA.setText(itemA.getName());
                tvNameB.setText(itemB.getName());

                if(!TextUtils.isEmpty(itemA.getShitNo())){
                    tvNoA.setText(itemA.getShitNo()+"号");
                }else{
                    tvNoA.setText("");
                }

                if(!TextUtils.isEmpty(itemB.getShitNo())){
                    tvNoB.setText(itemB.getShitNo()+"号");
                }else{
                    tvNoB.setText("");
                }


                layout.addView(view);

            }

            if(list.size() % 2 > 0){
                PlayerVo item = list.get(list.size()-1);
                View view = View.inflate(mContext,R.layout.layout_team_member_item,null);
                CircularImage ivLogoA = (CircularImage) view.findViewById(R.id.iv_logo_a);
                TextView tvNameA= (TextView)view.findViewById(R.id.tv_name_a);
                TextView tvNoA= (TextView)view.findViewById(R.id.tv_no_a);
                LinearLayout layoutB = (LinearLayout)view.findViewById(R.id.layout_b);

                layoutB.setVisibility(View.INVISIBLE);
                if(!TextUtils.isEmpty(item.getPhoto())){
                    ILFactory.getLoader().loadNet(ivLogoA, item.getPhoto(), null);
                }else{
                    ivLogoA.setImageResource(R.drawable.ic_my_head);
                }

                tvNameA.setText(item.getName());

                if(!TextUtils.isEmpty(item.getShitNo())){
                    tvNoA.setText(item.getShitNo()+"号");
                }else{
                    tvNoA.setText("");
                }

                layout.addView(view);
            }
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_type)
        TextView tvType;

        @BindView(R.id.layout_content)
        LinearLayout layoutContent;




        public ViewHolder(View itemView) {
            super(itemView);
            KnifeKit.bind(this, itemView);
        }
    }
}
